package dao;

import entity.Departments;
import util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDao {
    public List<Departments> findAll(){
        List<Departments> dts=new ArrayList<>();
        ResultSet rs= DBUtil.find("select * from Departments");
        ResultSetToList(dts, rs);
        return dts;
    }
    //重构函数 选中相同部分，点Refactor---refactor this----method
    private void ResultSetToList(List<Departments> dts, ResultSet rs) {
        try {
            while (rs.next()){
                Departments d=new Departments();
                d.setDepartment_id(rs.getString(1));
                d.setDepartment_name(rs.getString(2));
                d.setManager_id(rs.getString(3));
                d.setLocation_id(rs.getString(4));
                dts.add(d);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
